package com.ylyq.ylxx.context;


import com.ylyq.ylxx.constants.ShiroConstant;

import java.util.HashMap;
import java.util.Map;

public class FilterContextHandler {

    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    public static Map<String, Object> getAll() {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
        }
        return map;
    }

    public static void setAll(Map<String, Object> objectMap) {
        threadLocal.set(objectMap);
    }

    public static String getUserID() {
        Object value = get(ShiroConstant.CONTEXT_USER_ID);
        return returnObjectValue(value);
    }

    public static String getUsername() {
        Object value = get(ShiroConstant.CONTEXT_USER_NAME);
        return returnObjectValue(value);
    }

    public static String getToken() {
        Object value = get(ShiroConstant.TOKEN);
        return returnObjectValue(value);
    }

    public static void setToken(String token) {
        set(ShiroConstant.TOKEN, token);
    }
    //巨量accessToken
    public static String getOceaneToken() {
        Object value = get(ShiroConstant.OceaneToken);
        return returnObjectValue(value);
    }

    public static void setOceaneToken(String token) {
        set(ShiroConstant.OceaneToken, token);
    }
    //鲁班accessToken
    public static String getLubanToken() {
        Object value = get(ShiroConstant.LubanToken);
        return returnObjectValue(value);
    }

    public static void setLubanToken(String token) {
        set(ShiroConstant.LubanToken, token);
    }

    public static String getTokenType() {
        Object value = get(ShiroConstant.TOKEN_TYPE);
        return returnObjectValue(value);
    }

    public static void setTokenType(String type) {
        set(ShiroConstant.TOKEN_TYPE, type);
    }

    public static void setUserID(String userID) {
        set(ShiroConstant.CONTEXT_USER_ID, userID);
    }

    public static void setUsername(String username) {
        set(ShiroConstant.CONTEXT_USER_NAME, username);
    }

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

    public static void remove() {
        threadLocal.remove();
    }

}
